<?php
require('library/phpword/PHPWord.php');
class ScheduleDocx extends PHPWord
{
	protected $footerText = 'Gedownload van http://www.vasp.nl';
	
	protected $gameCount = 0;
	
	protected $lastTime = null;
	
	public function addPageWithTitleAndLocation($title, $location)
	{		
		$section = $this->createSection();
		$section->addText($title, array('name'=>'Arial', 'size'=>13, 'bold'=>true));
		$section->addText('', array('name'=>'Arial', 'size'=>13, 'bold'=>true));
		$section->addText('Locatie: '.$location, array('name'=>'Arial', 'size'=>10, 'bold'=>true));
	}
	
	
	/**
	 * @param string $text
	 * @return void
	 */
	public function setFooterText($text)
	{
		$this->footerText = $text;
	}
	
	
	public function AddPage()
	{
		$this->gameCount = 0;
		$this->lastTime = null;
	}
	
	/**
	 * @return string
	 */
	public function getFooterText()
	{
		return $this->footerText;
	}
	
	public function addFieldTitle($field)
	{
		
	}
	
	public function addGamesTableHeader($includeField = true)
	{
		
	}
	
	public function renderGamesPage(array $aGames, $includeField = true)
	{
		$this->addGamesTableHeader($includeField);
		
		$fieldsGames = array();
		$fields = 0;
		foreach($aGames as $oGame)
		{			
			$this->addGameRow($oGame, $includeField);
			$oGame = Game::getByNefubId($oGame->nefub_id);
			if($includeField)
			{
				$field = $oGame->field ? $oGame->field : 1;				
				if(!is_array($fieldsGames[$field]))
				{
					$fieldsGames[$field] = array();
					$fields++;
				}
				$fieldsGames[$field][] = $oGame; 			
			}
		}
		
		if($fields <= 1)
		{
			$fieldsGames = array();
		}
		ksort($fieldsGames);		
		return $fieldsGames;
	}
	
	public function addGameRow(Game $oGame, $includeField = true)
	{
		$oGame = Game::getByNefubId($oGame->nefub_id);
		$field = $oGame->field ? $oGame->field : 1;
		
		$gameType = $oGame->getCompetition()->getGender()->name .' '. $oGame->getCompetition()->getGenre()->name;
		$team1 = utf8_decode($oGame->getTeam1()->name);
		$team2 = utf8_decode($oGame->getTeam2()->name);
		
		if($oGame->getFirstReferee())
		{
			$oGame = new Game($oGame->getId());
			$oReferee = $oGame->getFirstReferee();
			$referee = utf8_decode($oReferee->getName());
			
			if(!$oReferee->getPerson())
			{			
				$referee .= ' ('. $oReferee->getTeam()->getCompetition()->getGender()->name;
				$referee .= ' '. $oReferee->getTeam()->getCompetition()->getGenre()->name .')';
			}
		}
		else
		{
			$referee = 'Onbekend';
		}
		
		$section = $this->createSection();
		$section->addText($oGame->getFormattedTime(), array('name'=>'Arial', 'size'=> 11, 'bold'=> false));
		$section->addText($team1, array('name'=>'Arial', 'size'=> 10, 'bold'=> true));
		$section->addText($team2, array('name'=>'Arial', 'size'=> 10, 'bold'=> true));
		$section->addText($gameType, array('name'=>'Arial', 'size'=> 10, 'bold'=> false));
		if($includeField)
		{
			$section->addText($field, array('name'=>'Arial', 'size'=> 11, 'bold'=> false));
		}
		$section->addText($team2, array('name'=>'Arial', 'size'=> 10, 'bold'=> true));
		$section->addText('', array('name'=>'Arial', 'size'=> 14));
	}
	
	public function output()
	{
		// At least write the document to webspace:
		$objWriter = PHPWord_IOFactory::createWriter($this, 'Word2007');
		$tempname = 'renders/tempschedule_'.md5(rand(1, 100).'.docx');
		$objWriter->save($tempname);		
		$handle = fopen($tempname, 'r');
		$data = fread($handle, filesize($tempname));
		fclose($handle);
		echo $data;
		unlink($tempname);
	}
}